﻿Imports System.IO

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
            On Error Resume Next
            For Each Form As Windows.Forms.Form In My.Application.OpenForms
                Form.Dispose()
            Next
        End Sub

        Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup

         
            If Environment.CommandLine.ToLower(System.Globalization.CultureInfo.CurrentCulture).Contains("-totray") Then
                UseTray = True
            End If

            If UseTray Then

                'Get Options
                Dim BangbezhINI As New Ini 'Make a new INI File for bangbezh in memory
                Dim OptionsINI As New Ini 'Make a new INI File for options in memory

                Check()
                OptionsINI.Load(GetStartupPath() & "\Data\Options.Bbj") 'Load Data into it
                BangbezhFile = OptionsINI.GetStringField("Options", "Bangbezh")
                City = CShort(OptionsINI.GetNumberField("Options", "City")) 'Get City
                Core.Load()
                Core.GetPrayerTimes()
                FrmMain.WindowState = FormWindowState.Minimized
                FrmMain.ShowInTaskbar = False
                FrmMain.Hide()

                FrmMain.Tray.Visible = True
            End If
        End Sub

        Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            TempFileName = FileIO.FileSystem.GetTempFileName()
            e.BringToForeground = True
            FileIO.FileSystem.WriteAllText(TempFileName, Nothing, False)

        End Sub

        Private Sub MyApplication_UnhandledException(sender As Object, e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            e.ExitApplication = False
            DlgUnhandledError.ErrorMessage = e.Exception.Message
            DlgUnhandledError.ShowDialog()
        End Sub
    End Class


End Namespace

